#ifndef LIST_SELECTER_H
#define LIST_SELECTER_H

#include <vector>
#include <actable.h>
#include <selectable.h>
#include <button.h>

class ListSelecterAdapter : public Actable, public Selectable
{
public:
    virtual ~ListSelecterAdapter() = 0;

    virtual void select(bool select) override = 0;
    virtual void act() override = 0;
    virtual void draw() override = 0;
    virtual void setOnSelcetClick(std::function<void()> callBack) = 0;
    virtual Texture* getTexture() = 0;
    virtual void setAttribute(int x, int y, int width, int height) = 0;
};
inline ListSelecterAdapter::~ListSelecterAdapter(){}

class ListSelecter
{
private:
    int x, y, width, height, gap;
public:
    std::vector<ListSelecterAdapter*> actors;
    ListSelecterAdapter* active;
    int activeCustomIndex;

    ListSelecter() = delete;
    ListSelecter(int x, int y, int width, int height, int gap) : x(x), y(y), width(width), height(height), gap(gap) {}
    ~ListSelecter(){
        for(auto& actor : actors) delete actor;
    }

    void render() {
        for(auto& actor : actors) actor->act();
        for(auto& actor : actors) actor->draw();
    }

    // 重置选中状态为 false
    void resetsatte(){
        for(auto& actor : actors) actor->select(false);
    }

    ListSelecterAdapter* getActive(){
        return active;
    }

    int getActiveCustomIndex(){
        return activeCustomIndex;
    }

    void addActor(ListSelecterAdapter* actor){
        if(actors.empty()) {
            actor->select(true);
            active = actor;
            activeCustomIndex = 0 -2;
        }
        actor->setOnSelcetClick([this,actor](){
            resetsatte();
            actor->select(true);
            active = actor;
            for(int i=0; i<actors.size(); i++) if(active == actors.at(i)) activeCustomIndex = i-2;
        });
        actor->setAttribute(x, y + actors.size()*gap, width, height);
        actors.push_back(actor);
    }

private:
    void setActive(){
        // for(auto actor : actors) if(actor.)
    }
};

#endif